//
//  ChangeSpeedWithAction.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-7-20.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
//
//  自定义动作：变速度

#import "ChangeSpeedWithAction.h"

@implementation ChangeSpeedWithAction

@synthesize innerAction = innerAction_;
@synthesize accSpeed = accSpeed_;
@synthesize startSpeed = startSpeed_;
@synthesize finishUpTime = finishUpTime_;
@synthesize startDowmTime = startDowmTime_;

+(id) actionWithAction: (CCActionInterval*)action 
                 accSpeed:(float)accSpeed
            startSpeed:(float)rate
          finishUpTime:(float)fut
         startDowmTime:(float)sdt
{
    return [[[self alloc] initWithAction:action accSpeed:accSpeed startSpeed:rate finishUpTime:fut startDowmTime:sdt] autorelease];
}

-(id) initWithAction: (CCActionInterval*) action
               accSpeed:(float)accSpeed
          startSpeed:(float)rate
        finishUpTime:(float)fut
       startDowmTime:(float)sdt
{
    if( (self=[super init]) ) 
    {
		self.innerAction = action;
        self.accSpeed = accSpeed;
        self.startSpeed = rate;
        self.finishUpTime = fut;
        self.startDowmTime = sdt;
        curSpeed_ = startSpeed_;
	}
	return self;
}

-(id) copyWithZone: (NSZone*) zone
{
	CCAction *copy = [[[self class] allocWithZone: zone] initWithAction:[[innerAction_ copy] autorelease] accSpeed:accSpeed_ startSpeed:startSpeed_ finishUpTime:finishUpTime_ startDowmTime:startDowmTime_];
    return copy;
}

-(void) dealloc
{
	[innerAction_ release];
	[super dealloc];
}

-(void) startWithTarget:(id)aTarget
{
	[super startWithTarget:aTarget];
	[innerAction_ startWithTarget:target_];
}

-(void) stop
{
	[innerAction_ stop];
	[super stop];
}

-(void) step:(ccTime) dt
{
    if( firstTick_ ) 
    {
		firstTick_ = NO;
		elapsed_ = 0;
	} else
    {
        elapsed_ += dt;
    }
    
    if(elapsed_ < finishUpTime_)
    {
        curSpeed_ += (accSpeed_ * dt);
        curSpeed_ = curSpeed_ > 1 ? 1: curSpeed_;
    }
    else if(elapsed_ > startDowmTime_)
    {
        curSpeed_ -= (accSpeed_ * dt);
        curSpeed_ = curSpeed_ < 0 ? 0.1: curSpeed_;
    }
    
	[innerAction_ step: dt * curSpeed_];
}

-(BOOL) isDone
{
	return [innerAction_ isDone];
}

- (CCActionInterval *) reverse
{
	return [ChangeSpeedWithAction actionWithAction:[innerAction_ reverse] accSpeed:accSpeed_ startSpeed:startSpeed_ finishUpTime:finishUpTime_ startDowmTime:startDowmTime_];
}
@end
